package com.cy.jwt.interceptor;

import com.cy.jwt.utils.JwtUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {
    /**
     * 在目标方法执行之前执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authentication");
//        String token = request.getParameter("Authentication");
        System.out.println(token);
        if (token==null||"".equals(token)){
            throw new RuntimeException("please login");
        }
        Boolean flag = JwtUtils.isTokenExpired(token);
        System.out.println(flag);
        if(flag)
            throw new RuntimeException("login timeout,please login");

        return true;
    }
}
